package ren.chemi.image;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;


public class Main {

    public static void main(String[] args) throws Exception {
        // 基于图像搜索指纹的相识度计算
        test_hash_finger();
        test_orign_hash_finger();
        test_crop_orign_hash_finger();

        // 基于颜色空间的相似度计算
        test_orign_images();
        test_crop_images();
        test_crop_orign_images();
    }

    public static void  test_hash_finger() throws IOException {
        FingerPrint fp1 = new FingerPrint(ImageIO.read(new File("white_car2.jpg")));
        FingerPrint fp2 = new FingerPrint(ImageIO.read(new File("black_car.jpg")));
        FingerPrint fp3 = new FingerPrint(ImageIO.read(new File("white_car.jpg")));
        System.out.printf("Finger CROP WB : %f WW:%f \n", fp1.compare(fp2), fp1.compare(fp3));
    }

    public static void  test_orign_hash_finger() throws IOException {
        FingerPrint fp1 = new FingerPrint(ImageIO.read(new File("orign/white_car2.jpg")));
        FingerPrint fp2 = new FingerPrint(ImageIO.read(new File("orign/black_car.jpg")));
        FingerPrint fp3 = new FingerPrint(ImageIO.read(new File("orign/white_car.jpg")));
        System.out.printf("Finger ORIGN WB : %f WW:%f \n", fp1.compare(fp2), fp1.compare(fp3));
    }

    private static void test_crop_orign_hash_finger() throws IOException {
        String fileName1 = "orign/white_car2.jpg";
        String fileName2 = "orign/black_car.jpg";
        String fileName3 = "orign/white_car.jpg";
        BufferedImage image1 = ImageIO.read(new File(fileName1));
        image1 = Scalr.cropImage(image1, 454, 0, 900, 225);
        ImageIO.write(image1, "JPEG", new File("test1.jpg"));
        BufferedImage image2 = ImageIO.read(new File(fileName2));
        image2 = Scalr.cropImage(image2, 454, 0, 900, 225);
        ImageIO.write(image2, "JPEG", new File("test2.jpg"));
        BufferedImage image3 = ImageIO.read(new File(fileName3));
        image3 = Scalr.cropImage(image3, 454, 0, 900, 225);
        ImageIO.write(image3, "JPEG", new File("test3.jpg"));

        FingerPrint fp1 = new FingerPrint(image1);
        FingerPrint fp2 = new FingerPrint(image2);
        FingerPrint fp3 = new FingerPrint(image3);

        System.out.printf("Finger CROP ORIGN WB : %f WW:%f \n", fp1.compare(fp2), fp1.compare(fp3));
    }

    private static void test_crop_images() throws IOException {
        String fileName1 = "white_car2.jpg";
        String fileName2 = "black_car.jpg";
        String fileName3 = "white_car.jpg";
        BufferedImage image1 = ImageIO.read(new File(fileName1));
        BufferedImage image2 = ImageIO.read(new File(fileName2));
        BufferedImage image3 = ImageIO.read(new File(fileName3));

        int [] array1 = ImageColorHistogramSimilarity.getImageFeatherVector(image1);
        int [] array2 = ImageColorHistogramSimilarity.getImageFeatherVector(image2);
        int [] array3 = ImageColorHistogramSimilarity.getImageFeatherVector(image3);

        double rate1 = ImageColorHistogramSimilarity.colorHistogramSimilarity(array1, array2);
        double rate2 = ImageColorHistogramSimilarity.colorHistogramSimilarity(array1, array3);
        System.out.println("CROP WB :" + rate1 + "WW:" + rate2);
    }

    private static void test_orign_images() throws IOException {
        String fileName1 = "orign/white_car2.jpg";
        String fileName2 = "orign/black_car.jpg";
        String fileName3 = "orign/white_car.jpg";
        BufferedImage image1 = ImageIO.read(new File(fileName1));
        BufferedImage image2 = ImageIO.read(new File(fileName2));
        BufferedImage image3 = ImageIO.read(new File(fileName3));

        int [] array1 = ImageColorHistogramSimilarity.getImageFeatherVector(image1);
        int [] array2 = ImageColorHistogramSimilarity.getImageFeatherVector(image2);
        int [] array3 = ImageColorHistogramSimilarity.getImageFeatherVector(image3);

        double rate1 = ImageColorHistogramSimilarity.colorHistogramSimilarity(array1, array2);
        double rate2 = ImageColorHistogramSimilarity.colorHistogramSimilarity(array1, array3);
        System.out.println("ORIGN WB :" + rate1 + "WW:" + rate2);
    }

    private static void test_crop_orign_images() throws IOException {
        String fileName1 = "orign/white_car2.jpg";
        String fileName2 = "orign/black_car.jpg";
        String fileName3 = "orign/white_car.jpg";
        BufferedImage image1 = ImageIO.read(new File(fileName1));
        image1 = Scalr.cropImage(image1, 454, 0, 900, 225);
        ImageIO.write(image1, "JPEG", new File("test1.jpg"));
        BufferedImage image2 = ImageIO.read(new File(fileName2));
        image2 = Scalr.cropImage(image2, 454, 0, 900, 225);
        ImageIO.write(image2, "JPEG", new File("test2.jpg"));
        BufferedImage image3 = ImageIO.read(new File(fileName3));
        image3 = Scalr.cropImage(image3, 454, 0, 900, 225);
        ImageIO.write(image3, "JPEG", new File("test3.jpg"));

        int [] array1 = ImageColorHistogramSimilarity.getImageFeatherVector(image1);
        int [] array2 = ImageColorHistogramSimilarity.getImageFeatherVector(image2);
        int [] array3 = ImageColorHistogramSimilarity.getImageFeatherVector(image3);

        double rate1 = ImageColorHistogramSimilarity.colorHistogramSimilarity(array1, array2);
        double rate2 = ImageColorHistogramSimilarity.colorHistogramSimilarity(array1, array3);
        System.out.println("CROP ORIGN WB :" + rate1 + "WW:" + rate2);
    }
}
